async function dealData(url,tag){
    let data = [];
    let count  = 0;
    try {
        let res = await this.axios.get(url)
        count = res.data.count;
        if(!tag){
            let getGroup = (data, key) => {
                let groupsObj = {};
                for(let arr of data){
                    let value = arr[key];
                    groupsObj[value] = groupsObj[value] || [];
                    groupsObj[value].push(arr);
                }
                let groupsArr = [];
                for(let k in groupsObj){
                    groupsArr.push({
                        title: k,
                        total: groupsObj[k].length,
                        list: groupsObj[k],
                    });
                }
                return groupsArr;
            }
            data = getGroup(res.data.users, 'dpart_name');
            
        }else{
            data = res.data.users;
        }
    } catch (error) {
        console.error(error);
    }
    return {
        data,
        count,
    };
}

// 获取用户
export async function getUser(id) {
    let data = undefined;
    await this.axios.get(`/user/getUser/${id}`).then(res => {
        data = res.data.user[0];
    }).catch(err => {
        console.error(err);
    });
    return data;
}

// 联合查询 users union dparts
export async function getUsersUDparts(tag) {
    let res = await dealData.bind(this)(`/user/getUsersUdparts`,tag);
    return res;
}

// 获取所有部门
export async function getDparts() {
    let data = [];
    await this.axios.get(`/dparts`).then(res => {
        for (let dpart of res.data.dparts) {
            data.push({
                val: dpart.dpart_id,
                lab: dpart.dpart_name
            })
        }
    }).catch(err => {
        console.error(err);
    });
    return data;
}

// 删除用户 restful style route
export async function delUser(id) {
    return await this.axios.get(`/user/delUser/${id}`);
}

// 新增用户
export async function addUser(params) {
    return await this.axios({
        method: 'post',
        url: `/user/addUser`,
        params,
    })
}

// 修改用户
export async function updateUser(params) {
    return await this.axios({
        method: 'post',
        url: `/user/updateUser`,
        params,
    })
}

// 搜索用户
export async function searchUsers(kw,tag){
    let res = await dealData.bind(this)(`/user/searchUsers?kw=${kw}`,tag);
    return res;
}